package kr.pe.johnkim.app.mobile.util;

import kr.pe.johnkim.app.domain.MyHistoryMember;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author : johnkim <cale9797@gmail.com / Simple.ly Inc,>
 * @Date : 11. 12. 28
 */
public class MyHistoryEmail {
    // SLF4J Logger
    private Logger logger = LoggerFactory.getLogger(MyHistoryEmail.class);

    /**
     * 메일을 발송 한다.
     * 
     * @param myHistoryMember
     */
    protected void sendMyHistoryEmail(MyHistoryMember myHistoryMember) {
        logger.info("sendMyHistoryEmail");

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, false, "euc-kr");

            // 제목
            messageHelper.setSubject("MyHistory Mobile Service 가입을 환영 합니다.");
            // 내용
            String htmlContent = "<strong>안녕하세요</strong>, 반갑습니다.<br>"
                               + "저희 마이히스토리 모바일서비스를 이용해 주셔서 고맙습니다.<br>";
            messageHelper.setText(htmlContent, true);
            // 보내는 사람
            messageHelper.setFrom("help@myhistory.com", "Bettersoft Inc,.");
            // 받는 사람
            messageHelper.setTo(new InternetAddress(myHistoryMember.getEmail(), myHistoryMember.getNickName(), "euc-kr"));
        } catch (Throwable e) {
            logger.error("Fail to send mail : {}", e);
        }

        // 메일을 발송 한다.
        try {
            mailSender.send(mimeMessage);

            logger.info("Success to send mail.");
        } catch (MailException e) {
            logger.error("Fail to send mail : {}", e);
        }
    }

    /**
     * 파일을 첨부한 메일을 발송 한다.
     * 
     * @param myHistoryMember
     */
    protected void sendMyHistoryEmailWithAttachment(MyHistoryMember myHistoryMember) {
        logger.info("sendMyHistoryEmailWithAttachment");

        MimeMessage mimeMessage = mailSender.createMimeMessage();
    }
    
    /**
     * Setter Injection.
     */
    private JavaMailSender mailSender;

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
}
